package com.motorhome.manage.controller;

import com.motorhome.base.dao.Pager;
import com.motorhome.base.easyui.EasyuiDataGridResult;
import com.motorhome.base.util.Result;
import com.motorhome.manage.domain.Car;
import com.motorhome.manage.service.CarService;
import com.motorhome.manage.service.CarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.*;

/**
 * @author: John
 */
@Controller
@RequestMapping("/cartype")
public class CarTypeController {

    @Autowired
    private CarTypeService carTypeService;

    @RequestMapping("/indextype")
    public String index(HttpServletRequest request) {
        return "cartype/indextype";
    }

    private String sendStr = "SendString";
    private String netAddress = "127.0.0.1";
    private final int PORT_NUM = 5066;

    private DatagramSocket datagramSocket;
    private DatagramPacket datagramPacket;


    @RequestMapping(value = "/getCarListType", method = RequestMethod.POST)
    public @ResponseBody String getCarListType(HttpServletRequest request) {
        String page = request.getParameter("page");
        String rows = request.getParameter("rows");
        String type = request.getParameter("type");
        Pager<Car> pager = carTypeService.getCarListType(page, rows, type);
        return new EasyuiDataGridResult<>(pager.getTotalCount(), pager.getItems()).toJson();
    }


    @RequestMapping(value = "/closeDoor", method = RequestMethod.POST)
    public @ResponseBody String closeDoor(HttpServletRequest request) {

        String str = "";
        return UdpClient(str).toString();
    }

    @RequestMapping(value = "/closeCar", method = RequestMethod.POST)
    public @ResponseBody String closeCar(HttpServletRequest request) {

        String str = "";
        return UdpClient(str).toString();
    }


    public Result  UdpClient(String strCommand){
        try {

            /*** 发送数据***/
            // 初始化datagramSocket,注意与前面Server端实现的差别
            datagramSocket = new DatagramSocket();
            // 使用DatagramPacket(byte buf[], int length, InetAddress address, int port)函数组装发送UDP数据报
            byte[] buf = sendStr.getBytes();
            InetAddress address = InetAddress.getByName(netAddress);
            datagramPacket = new DatagramPacket(buf, buf.length, address, PORT_NUM);
            // 发送数据
            datagramSocket.send(datagramPacket);

            /*** 接收数据***/
            byte[] receBuf = new byte[1024];
            DatagramPacket recePacket = new DatagramPacket(receBuf, receBuf.length);
            datagramSocket.receive(recePacket);

            String receStr = new String(recePacket.getData(), 0 , recePacket.getLength());
            System.out.println("Client Rece Ack:" + receStr);
            System.out.println(recePacket.getPort());

            return new Result(0, "成功");

        } catch (SocketException e) {
            e.printStackTrace();
            return new Result(1, "失败");
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return new Result(1, "失败");
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(1, "失败");
        } finally {
            // 关闭socket
            if(datagramSocket != null){
                datagramSocket.close();
            }
        }
    }
}
